package com.ant.business.system.service.impl;

import com.ant.business.system.entity.UserRole;
import com.ant.business.system.mapper.UserRoleDao;
import com.ant.business.system.service.UserRoleService;
import com.ant.business.system.vo.UserVo;
import com.ant.common.exceptions.BusinessException;
import com.ant.common.utils.FastMap;
import com.ant.common.utils.UserThreadLocal;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 用户角色管理Service实现类
 * @author
 * @description
 * @create 2021-03-22 14:25:56
 */
@Service("userRoleService")
@Transactional(rollbackFor = Throwable.class)
public class UserRoleServiceImpl extends ServiceImpl<UserRoleDao, UserRole> implements UserRoleService {

    private final UserRoleDao userRoleDao;

    public UserRoleServiceImpl(UserRoleDao userRoleDao) {
        this.userRoleDao = userRoleDao;
    }

    /**
     * 分页查询列表
     * @param page
     * @param limit
     * @return
     */
    @Override
    public PageInfo<UserRole> getList(int page, int limit) {
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        PageHelper.startPage(page, limit);
        List<UserRole> list = userRoleDao.selectList(wrapper);
        return new PageInfo<UserRole>(list);
    }

    /**
     * 新增信息
     * @param userRole
     */
    @Override
    @SneakyThrows
    public void saveOne(UserRole userRole) {
        int count = userRoleDao.insert(userRole);
        if (count!=1){
            throw new BusinessException("保存失败");
        }
    }

    /**
     * 更新信息
     * @param userRole
     */
    @Override
    @SneakyThrows
    public void update(UserRole userRole) {
        int count = userRoleDao.updateById(userRole);
        if (count!=1){
            throw new BusinessException("更新失败");
        }
    }

    /**
     * 删除信息
     * @param primaryKey
     */
    @Override
    @SneakyThrows
    public void delete(String primaryKey) {
        int count = userRoleDao.deleteById(primaryKey);
        if (count!=1){
            throw new BusinessException("删除失败");
        }
    }

    /**
     * 更新用户角色
     * @param param
     */
    @Override
    @SneakyThrows
    public void updateUserRole(FastMap param) {
        UserVo user = UserThreadLocal.getUser();
        int roleId = param.getInt("roleId");

        Object userIdsObj = param.get("userIds");
        List<Integer> userIds = new ArrayList<>();
        if (Objects.nonNull(userIdsObj) && userIdsObj instanceof List) {
            userIds = (List<Integer>) userIdsObj;
        }

        if (roleId == 0) {
            throw new BusinessException("请选择角色");
        }

        if (userIds.size() == 0) {
            throw new BusinessException("请选择用户");
        }
        this.remove(new QueryWrapper<UserRole>().eq("unit_id", user.getUnitId()).eq("role_id", roleId));

        List<UserRole> userRoleList = new ArrayList<>();
        for (Integer userId : userIds) {
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            userRole.setUnitId(user.getUnitId());
            userRoleList.add(userRole);
        }

        if (userRoleList.size()>0){
            boolean save = this.saveBatch(userRoleList);
            if (!save){
                throw new BusinessException("写入用户角色时失败");
            }
        }
    }
}
